package org.example.completed.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.example.completed.domain.Comment;
import org.example.completed.model.dto.CommentAndAvatarDTO;

import java.util.List;

@Mapper
public interface CommentMapper extends BaseMapper<Comment> {

    // 获取所有评论，并返回评论者的头像和用户名
    @Select("SELECT c.id, c.username, u.img AS avatarUrl, c.content, c.image_url, c.created_at " +
            "FROM comment c " +
            "JOIN user u ON c.username = u.username")
    @Results({
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "imageUrl", column = "image_Url")
    })
    List<Comment> getAllComments();

    // 插入评论
    @Insert("INSERT INTO comment(username, content, image_url) VALUES(#{username}, #{content}, #{imageUrl})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addComment(Comment comment);

    // 根据用户名获取评论，并返回头像
    @Select("SELECT c.id, c.username, u.img AS avatarUrl, c.content, c.image_url, c.created_at " +
            "FROM comment c " +
            "JOIN user u ON c.username = u.username " +
            "WHERE c.username = #{username}")
    @Results({
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "imageUrl", column = "image_url"),
            @Result(property = "avatarUrl", column = "avatarUrl")
    })
    CommentAndAvatarDTO getCommentsByUsernameWithAvatar(@Param("username") String username);
}